<template>
	<view>
		<!-- 头部搜索框 -->
		<search :placeholder="$t('homepage.WarehouseManage.box')" />
		<!-- 搜索无记录 -->
		<zy-empty v-if="this.infoList.length==0"></zy-empty>
		<!-- 查询表单 -->
		<view v-else>
			<list :infoList="infoList"></list>
			<bottomBtn fixed :leftText="$t('public.reset')" :rightText="$t('homepage.WarehouseManage.SP')"
				@rightBtn="selectHandler" @leftBtn="resetBtn">
			</bottomBtn>
		</view>
		<!-- 筛选弹窗 -->
		<zy-popup ref="zyPopup" type='dialog' :title="$t('homepage.WarehouseManage.SPselect')" @cancel="cancel"
			@confirm="submit" closeable :closeOnClickOverlay="false">
			<zy-forms :rules="rules" :model="model" labelWidth="80">
				<zy-form-item :label="$t('public.storage')" required>
					<zy-input v-model="model.storage" :placeholder="$t('public.pleaseInput')+$t('public.storage')"
						@confirm="storageSearch" sufSlot>
						<i class="iconfont icon-saoyisao" @click="scanStorage"></i>
					</zy-input>
				</zy-form-item>
				<zy-form-item :label="$t('public.position')" required>
					<zy-input v-model="model.position" @confirm="positionSearch"
						:placeholder="$t('public.pleaseInput')+$t('public.position')" sufSlot>
						<i class="iconfont icon-saoyisao" @click="scanPosition"></i>
					</zy-input>
				</zy-form-item>
			</zy-forms>
		</zy-popup>
		<!-- 调拨提交二次确认 -->
		<zy-popup ref="popup" :title="$t('homepage.WarehouseManage.allocationConfirm')" type="dialog"
			@cancel="cancelBtn" @confirm="submitBtn">
			{{$t('public.isSure') }}{{$t('homepage.WarehouseManage.allocationConfirm') }}{{$t('public.question')}}
		</zy-popup>
	</view>
</template>
<script>
	import {
		getStorehouseDetailByCode,
		positionGetEntityByCode,
		storageGetEntityByCode,
		storeAllocation,
	} from "@/api/homepage.js"
	import list from '../components/infoList.vue';
	export default {
		components: {
			list,
		},
		data() {
			return {
				model: {
					position: '',
					storage: '',
				},
				codeId: '',
				infoList: [],
				rules: {
					position: {
						required: false,
						message: this.$t('public.pleaseInput') + this.$t('public.position'),
						trigger: 'blur'
					},
					storage: {
						required: true,
						message: this.$t('public.pleaseInput') + this.$t('public.storage'),
						trigger: 'blur'
					}
				},
				infoLists: [],
				codeIds: '',
				positionId: '',
				storageId: '',
			}
		},
		onLoad() {
			this.getscancodedate()
		},
		onUnload() {
			// 移除监听事件      
			uni.$off('scancodedate')
			uni.$off('scancodepopdate')
		},
		methods: {
			//主页获取箱号扫描事件
			getscancodedate() {
				// 移除监听“添加扫描”弹窗事件
				uni.$off('scancodepopdate')
				//PDA扫描主页指示单号
				var _this = this
				uni.$on('scancodedate', function(data) {
					_this.searchList(data.code.replace(/\s/g, ""))
				})
			},
			//“弹窗储位”获取扫描事件
			getscancodepopdate() {
				// 移除监听“指示单号”事件
				uni.$off('scancodedate')
				//PDA扫描“添加扫描”弹窗事件
				var _this = this
				uni.$on('scancodepopdate', function(data) {
					_this.model.storage = data.code.replace(/\s/g, "")
					_this.storageSearch()
				})
			},
			//查询物料编码事件接口
			searchList(e) {
				if (!e) {
					uni.showToast({
						title: this.$t('public.pleaseInput') + this.$t('homepage.WarehouseManage.box'),
						icon: "none",
						duration: 3000
					})
				} else {
					getStorehouseDetailByCode({
						code: e
					}).then((res) => {
						console.log(res)
						if (res.code == 0) {
							if (res.data.status == 1) {
								if (this.infoList.length == 0) {
									this.infoList.push(res.data)
								} else {
									if (this.infoList.findIndex(item => item.barcode === e) !== -1) {
										uni.showToast({
											title: this.$t('public.searchRepeat'),
											icon: "none",
											duration: 3000
										})
									} else {
										this.infoList.push(res.data)
									}
								}
							} else {
								uni.showToast({
									title: this.$t('homepage.WarehouseManage.thiscode') + res.data
										.statusText,
									icon: "none",
									duration: 3000
								})
							}
						} else {
							uni.showToast({
								title: res.message,
								icon: "none",
								duration: 3000
							})
						}
					}).catch((res) => {
						uni.showToast({
							title: res.message,
							icon: "none",
							duration: 3000
						})
					});
				}
			},
			// 底部仓/储按钮事件
			selectHandler() {
				this.$refs.zyPopup.open()
				this.getscancodepopdate()
			},
			//仓位扫描事件
			scanPosition() {
				var mpaasScanModule = uni.requireNativePlugin("Mpaas-Scan-Module")
				mpaasScanModule.mpaasScan({
						// 扫码识别类型，参数可多选，qrCode、barCode，不设置，默认识别所有
						'scanType': ['qrCode', 'barCode'],
						// 是否隐藏相册，默认false不隐藏
						'hideAlbum': false
					},
					(ret) => {
						if (ret.resp_result) {
							this.model.position = ret.resp_result;
							this.positionSearch()
						}
					})
			},
			//储位扫描事件
			scanStorage() {
				var mpaasScanModule = uni.requireNativePlugin("Mpaas-Scan-Module")
				mpaasScanModule.mpaasScan({
						// 扫码识别类型，参数可多选，qrCode、barCode，不设置，默认识别所有
						'scanType': ['qrCode', 'barCode'],
						// 是否隐藏相册，默认false不隐藏
						'hideAlbum': false
					},
					(ret) => {
						if (ret.resp_result) {
							this.model.storage = ret.resp_result;
							this.storageSearch()
						}
					})
			},
			//仓位接口
			positionSearch() {
				console.log(this.model.position)
				positionGetEntityByCode({
					code: this.model.position
				}).then((res) => {
					if (res.code == 0) {
						this.positionId = res.data.id
					} else {
						uni.showToast({
							title: res.message,
							icon: "none",
							duration: 3000
						})
					}
				}).catch((res) => {
					uni.showToast({
						title: res.message,
						icon: "none",
						duration: 3000
					})
				});
			},
			//储位接口
			storageSearch() {
				console.log(this.model.storage)
				storageGetEntityByCode({
					code: this.model.storage
				}).then((res) => {
					if (res.code == 0) {
						this.model.position = res.data.warehousePosition.code
						this.positionSearch()
						this.storageId = res.data.id
					} else {
						uni.showToast({
							title: res.message,
							icon: "none",
							duration: 3000
						})
					}
				}).catch((res) => {
					uni.showToast({
						title: res.message,
						icon: "none",
						duration: 3000
					})
				});
			},
			// 页面重置
			resetBtn() {
				uni.redirectTo({
					url: "/pages/HomePage/WarehouseManage/StorageManagement/Allocation/index"
				})
			},
			// 仓/储选择提交
			submit() {
				if (!this.model.position || !this.model.storage) {
					uni.showToast({
						title: this.$t('homepage.WarehouseManage.SPnull'),
						icon: "none",
						duration: 3000
					})
				} else {
					this.$refs.popup.open()
				}
			},
			// 仓/储选择取消
			cancel() {
				this.model.position = ''
				this.model.storage = ''
				this.positionId = ''
				this.storageId = ''
			},
			//调拨提交二次确认
			submitBtn() {
				for (var i = 0; i < this.infoList.length; i++) {
					this.codeId += this.infoList[i].id + ','
				}
				this.codeId = this.codeId.slice(0, this.codeId.length - 1)
				storeAllocation({
					warehouseInId: this.codeId,
					targetPositionId: this.positionId,
					targetStorageId: this.storageId
				}).then((res) => {
					if (res.code == 0) {
						uni.redirectTo({
							url: "/pages/HomePage/WarehouseManage/StorageManagement/Allocation/index"
						})
						uni.showToast({
							title: res.message,
							icon: "none",
							duration: 3000
						})
					} else {
						uni.showToast({
							title: res.message,
							icon: "none",
							duration: 3000
						})
					}
				}).catch((res) => {
					uni.showToast({
						title: res.message,
						icon: "none",
						duration: 3000
					})
				});
			},
			//入库调拨提交二次确认取消
			cancelBtn() {
				this.$refs.zyPopup.open()
				this.$refs.popup.close()
			},
		}
	}
</script>
<style lang="scss" scoped>
</style>